﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LongGi.TingkatKebocoranModule.CommModel
{
    public static class MyDataTableExtensions
    {
        public static decimal GetColumnAbsMax(DataTable dt, string columnName)
        {
            return dt.AsEnumerable()
                    .Where(row => !row.IsNull(columnName))
                    .Max(row => Math.Abs(Convert.ToDecimal(row[columnName])));
        }

        public static decimal GetColumnAbsSum(DataTable dt, string columnName)
        {
            return dt.AsEnumerable()
                    .Where(row => !row.IsNull(columnName))
                    .Sum(row => Math.Abs(Convert.ToDecimal(row[columnName])));
        }

        public static decimal GetColumnAbsAverage(DataTable dt, string columnName)
        {
            return dt.AsEnumerable()
                    .Where(row => !row.IsNull(columnName))
                    .Average(row => Math.Abs(Convert.ToDecimal(row[columnName])));
        }

        // 同时获取所有统计信息
        public static (decimal max, decimal sum, decimal avg) GetColumnAbsStats(DataTable dt, string columnName)
        {
            var validRows = dt.AsEnumerable()
                             .Where(row => !row.IsNull(columnName))
                             .Select(row => Math.Abs(Convert.ToDecimal(row[columnName])))
                             .ToList();

            if (!validRows.Any())
                return (0, 0, 0);

            return (validRows.Max(), validRows.Sum(), validRows.Average());
        }
    }

}
